package com.time.hellotime.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Recycler Adapter 的基类
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public abstract class BaseRecAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    protected static int TYPE_COUNTER = 1;
    protected Context mContext;
    protected LayoutInflater inflater;
    public OnItemClickListener onItemClickListener;

    // 数据源
    protected List<T> mList;
    protected int viewType;

    public BaseRecAdapter(Context mContext, List<T> list, int viewTypeCount) {
        this.mContext = mContext;
        this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setData(list);
        TYPE_COUNTER = viewTypeCount;
    }

    private void setData(List<T> list) {
        if (list == null) {
            this.mList = new ArrayList<T>();
        } else {
            this.mList = list;
        }
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public int getViewType() {
        return viewType;
    }

    public void updateData(List<T> list) {
        setData(list);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return this.mList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public T getItem(int position) {
        return this.mList.get(position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(View view, int position, T t);
    }
//    private boolean canNotifyData() {
//        if (rvCartList.getScrollState() == RecyclerView.SCROLL_STATE_IDLE || rvCartList.isComputingLayout() == false) {
//            return true;
//        }
//        return false;
//    }
}
